﻿using System.Collections.Generic;

namespace HtmlParser
{
	class HtmlElement : IHtmlElement
	{
		public HtmlElement(string name)
		{
			this._Name = name;
		}
		private readonly string _Name;
		public string Name { get { return _Name; } }
		public string Html { get; set; }
		public IHtmlElement First { get; set; }
		public IHtmlElement Previous { get; set; }
		public IHtmlElement Next { get; set; }
		public IHtmlElement ParentNode { get; set; }
		internal HtmlTagType TagType { get; set; }
		public HtmlElementType Type
		{
			get
			{
				switch (TagType)
				{
					case HtmlTagType.Comment:
						return HtmlElementType.Comment;
					case HtmlTagType.Text:
						return HtmlElementType.Text;
					case HtmlTagType.DocType:
						return HtmlElementType.Doctype;
					case HtmlTagType.Unknown:
						return HtmlElementType.Unknown;
					default:
						return HtmlElementType.Element;
				}
			}
		}

		public IHtmlElement[] Children
		{
			get
			{
				List<IHtmlElement> list = new List<IHtmlElement>();
				var f = First;
				while (f != null)
				{
					list.Add(f);
					f = f.Next;
				}
				return list.ToArray();
			}
		}

		public override string ToString()
		{
			return Html ?? _Name;
		}
	}
}
